//package com.example.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.example.entity.Message;
//import com.example.entity.User;
//import com.example.mapper.MessageMapper;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.List;
//
///**
// * @author will_jay
// * #前台聊天室应用service类
// * 注意：这里的service类继承ServiceImpl是mybatis-plus中的操作，里面封装了常用的mybatis增删改查操作
// * 只要继承，那么mapper类和mapper配置文件都不需要写操作语句，只需要定义就可以直接使用这些操作了
// */
//@Service
//public class MessageService extends ServiceImpl<MessageMapper, Message> {
//
//    @Resource
//    private MessageMapper messageMapper;
//
//    @Resource
//    private UserService userService;
//
//    /**
//     * 自定义的方法 找到交流的 另一个对象（交流的消息、头像，存在Message对象中）
//     * @return
//     */
//    public List<Message> findByForeign() {
//        LambdaQueryWrapper<Message> queryWrapper = Wrappers.<Message>lambdaQuery().eq(Message::getForeignId, 0).orderByDesc(Message::getId);
//        List<Message> list = list(queryWrapper);
//        for (Message Message : list) {
//            User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, Message.getUsername()));
//            Message.setAvatar("http://localhost:9999/files/" + one.getAvatar());
//            Long parentId = Message.getParentId();
//            list.stream().filter(c -> c.getId().equals(parentId)).findFirst().ifPresent(Message::setParentMessage);
//        }
//        return list;
//    }
//
//}


package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Message;
import com.example.entity.User;
import com.example.mapper.MessageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService extends ServiceImpl<MessageMapper, Message> {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserService userService;

    public List<Message> findByForeign() {
        LambdaQueryWrapper<Message> queryWrapper = Wrappers.<Message>lambdaQuery().eq(Message::getForeignId, 0).orderByDesc(Message::getId);
        List<Message> list = list(queryWrapper);
        for (Message Message : list) {
            User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, Message.getUsername()));
            Message.setAvatar("http://localhost:9999/files/" + one.getAvatar());
            Long parentId = Message.getParentId();
            list.stream().filter(c -> c.getId().equals(parentId)).findFirst().ifPresent(Message::setParentMessage);
        }
        return list;
    }

}

